//
//  NetworkTools.swift
//  Swift微博
//
//  Created by 陈竹 on 16/2/24.
//  Copyright © 2016年 陈竹. All rights reserved.
//

import UIKit
import AFNetworking
enum CZRequestMethod : String{
    case GET="GET"
    case POST="POST"
}
class NetworkTools: AFHTTPSessionManager {
    
    let Appkey="3221981861"
    let AppSecret="a29e6b3a33356ddb11093caa05466f9f"
    let AppRedirectUri="http://baidu.com"
    
    //网络工具类的单例
    static let sharedTools: NetworkTools = {
        let tools=NetworkTools(baseURL: nil)
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
   
    
    func request(method:CZRequestMethod,URLString:String,parametes:[String:AnyObject]?,finished:(result:[String:AnyObject]?)->()){
        //成功回调闭包
        let success = {(task:NSURLSessionDataTask, responeseObject:AnyObject?) -> Void in
            //如果返回数值是值对返回数据，否则返回nil
            if let result=responeseObject as? [String : AnyObject]{
                finished(result: result)
            }else{
                print("数据格式错误")
            finished(result: nil)
            }
        }
        //失败回调闭包
        let failure={(task:NSURLSessionDataTask?,error: NSError) -> Void
            in
            print("网络请求错误")
            finished(result: nil)
        }
        
        
        if method == .GET{
            //get请求
        GET(URLString, parameters: parametes, progress: nil, success: success, failure: failure)
        }else{
           // post请求
       POST(URLString, parameters: parametes, progress: nil, success: success, failure: failure)
        }
    
    }
}
